﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace WebservicePA
{
    /// <summary>
    /// Summary description for PAService
    /// </summary>
    [WebService(Namespace = "http://KnockIT.me/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PAService : System.Web.Services.WebService
    {
        ServiceResponse objServiceResponse = new ServiceResponse();
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUser(string Name)
        {
            try
            {
                if (Name == "error")
                    throw new Exception("Error occurred.");
                User objUser = new User();
                objUser.FirstName = Name;
                objUser.Age = "21";
                objUser.MobileNo = "9999999999";
                objUser.Email = Name + "@sample.com";
                objServiceResponse.IsSucess = true;
                objServiceResponse.ResponseString = new JavaScriptSerializer().Serialize(objUser);
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
            catch (Exception ex)
            {
                objServiceResponse.ErrorCode = "E001";
                objServiceResponse.IsSucess = false;
                objServiceResponse.Error = ex.Message;
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SaveUser(User user)
        {
            try
            {
                objServiceResponse.IsSucess = true;
                objServiceResponse.ResponseString = "Saved user details sucessfully!";
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
            catch (Exception ex)
            {
                objServiceResponse.ErrorCode = "E001";
                objServiceResponse.IsSucess = false;
                objServiceResponse.Error = ex.Message;
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetMessages(int MessageCount)
        {
            try
            {
                Message[] objMsg = new Message[MessageCount];
                for (int i = 0; i<MessageCount; i++)
                {
                objMsg[i] = new Message();
                objMsg[i].dateTime = DateTime.Now.AddMinutes(i * 2).ToString("MM/dd/yyyy HH:mm");
                objMsg[i].message = "Test Message " + Convert.ToString(i+1);

                }
                objServiceResponse.ResponseString = new JavaScriptSerializer().Serialize(objMsg);
                objServiceResponse.IsSucess = true;
                //objServiceResponse.ResponseString = "Saved user details sucessfully!";
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
            catch (Exception ex)
            {
                objServiceResponse.ErrorCode = "E001";
                objServiceResponse.IsSucess = false; 
                objServiceResponse.Error = ex.Message;
                return new JavaScriptSerializer().Serialize(objServiceResponse);
            }
        }

    }

    public class ServiceResponse
    {
        public string ErrorCode { get; set; }
        public string Error { get; set; }
        public bool IsSucess { get; set; }
        public string ResponseString { get; set; }
    }

    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Age { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string MobileNo { get; set; }
        public string DOB { get; set; }
    }

    public class Message
    {
        public string message { get; set; }
        public string dateTime { get; set; }
    }
}
